<?php
require('config.php');

        $user_id=$_REQUEST['user_id'];
        $youhui_id=$_REQUEST['youhui_id'];
        $time=time();
		$sql="select * from `".$oto."_youhui_user_link` where user_id='{$user_id}' and youhui_id='{$youhui_id}' and u_is_effect='1'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$user_link[]=$row;//将取得的所有数据赋值给person_info数组
		}
		$sql="select * from `".$oto."_youhui` where id='{$youhui_id}'";
		$result=$db->query($sql);
		while($row=$result->fetch_assoc()){
			$youhui_info[]=$row;//将取得的所有数据赋值给person_info数组
		}
		if(!isset($youhui_info))
		{
			$data[0]['pan']=-1;
			$data[0]['msg']="优惠券不存在";
			echo json_encode($data);
			die();
		}
		if(!isset($user_link))
		{
			if($youhui_info[0]['total_num']==$youhui_info[0]['user_count'])
			{
				$data[0]['pan']=-1;
            	$data[0]['msg']='已被领完了';
            	echo json_encode($data);
            	die();
			}
			$shop_id=$youhui_info[0]['supplier_id'];
			$sql="INSERT INTO ".$oto."_youhui_user_link (`youhui_id`,`user_id`,`shop_id`,`surplus`,`get_time`,`todayget`,`u_is_effect`) VALUES ('$youhui_id','$user_id','$shop_id','1','$time','1','1')";
            $result=$db->query($sql);
            if($result)
            {
            	$data[0]['pan']=1;
            	$sql="update ".$oto."_youhui set user_count=user_count+1 where id='{$youhui_id}'";
				$result=$db->query($sql);
            }
            else
            {
            	$data[0]['pan']=-1;
            	$data[0]['msg']='领取异常';
            }
		}
		else
		{
			$time2=date('Y-m-d',$time);
			$time3=date('Y-m-d',$user_link[0]['get_time']);
			if($time2==$time3)
			{
				if(($user_link[0]['todayget']+1)>$youhui_info[0]['user_limit'] || $youhui_info[0]['total_num']-1==$youhui_info[0]['user_count'])
				{
					$data[0]['pan']=-1;
	            	$data[0]['msg']='已被领完了';
	            	echo json_encode($data);
	            	die();
				}
				$surplus=$user_link[0]['surplus']+1;
				$todayget=$user_link[0]['todayget']+1;
				$sql="update ".$oto."_youhui_user_link set surplus='{$surplus}',get_time='{$time}',todayget='{$todayget}' where user_id='{$user_id}' and youhui_id='{$youhui_id}' and u_is_effect='1'";
				$result=$db->query($sql);
				if($result)
	            {
	            	$data[0]['pan']=1;
	            	$sql="update ".$oto."_youhui set user_count=user_count+1 where id='{$youhui_id}'";
					$result=$db->query($sql);
	            }
	            else
	            {
	            	$data[0]['pan']=-1;
	            	$data[0]['msg']='领取异常';
	            }
			}
			else
			{
				$surplus=$user_link[0]['surplus']+1;
				$sql="update ".$oto."_youhui_user_link set surplus='{$surplus}',get_time='{$time}',todayget='1' where user_id='{$user_id}' and youhui_id='{$youhui_id}' and u_is_effect='1'";
				$result=$db->query($sql);
				if($result)
	            {
	            	$data[0]['pan']=1;
	            	$sql="update ".$oto."_youhui set user_count=user_count+1 where id='{$youhui_id}'";
					$result=$db->query($sql);
	            }
	            else
	            {
	            	$data[0]['pan']=-1;
	            	$data[0]['msg']='领取异常';
	            }
			}
		}
		echo json_encode($data);
?>